using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
    public class tbHinhThucTTDAL
    {
        private SqlConnection cn = null;
        public tbHinhThucTTDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public void Insert(tbHinhThucTT hinhthuctt)
        {
            SqlCommand cm = new SqlCommand("sp_HinhThucTT_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenHinhThuc", hinhthuctt.TenHinhThuc);
            cm.ExecuteNonQuery();
        }

        public void Update(tbHinhThucTT hinhthuctt)
        {
            SqlCommand cm = new SqlCommand("sp_HinhThucTT_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdHinhThuc", hinhthuctt.IdHinhThuc);
            cm.Parameters.AddWithValue("@TenHinhThuc", hinhthuctt.TenHinhThuc);
            cm.ExecuteNonQuery();
        }

        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_HinhThucTT_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdHinhThuc", Id);
            cm.ExecuteNonQuery();
        }

        public tbHinhThucTTCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_HinhThucTT_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbHinhThucTTCollection hinhthucttcollection = new tbHinhThucTTCollection();
            while (dr.Read())
            {
                tbHinhThucTT hinhthuctt = new tbHinhThucTT();
                hinhthuctt.IdHinhThuc = int.Parse(dr.GetValue(0).ToString());
                hinhthuctt.TenHinhThuc = dr.GetValue(1).ToString();
                hinhthucttcollection.Add(hinhthuctt);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return hinhthucttcollection;
        }

    }
}
